package cn.smvc.sys.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import cn.smvc.common.utils.StringUtils;
import cn.smvc.sys.entity.SysUser;

public class LoginFilter implements Filter{
	private final static Logger logger = Logger.getLogger(LoginFilter.class);
	private Set<String> setURL = new HashSet<String>();
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		List<String> list = new ArrayList<String>();
		list.add("/coreServlet");
		list.add("/send/msg");
		list.add("/login");
		list.add("");
		list.add("");
		list.add("");
		list.add("");
		
		
		
		setURL = new HashSet<String>(list);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		String requestURL = req.getServletPath();
		logger.info("请求地址-->"  + requestURL);
		logger.info(!setURL.contains(requestURL));
		logger.info(requestURL.indexOf(".") < 0);
		
		if(!setURL.contains(requestURL) && requestURL.indexOf(".") < 0) {
			HttpSession session = req.getSession();
			SysUser user = (SysUser) session.getAttribute("user");
			if(user == null || StringUtils.isEmpty(user.getId())) {
				request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
			} else {
				logger.info("请求地址-->"  + req.getServletPath());
			}
		} else {
			
		}
		chain.doFilter(request, response);
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}


}
